var app = getApp();
var firstLogin = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    userInfo: {},
    hasUserInfo: false,
    loginTitle: "点击登录/注册",
    userIcon: '/images/defaultIcon.png',
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
    couponToalPrice: 0,
    disabled: false,
    backHidden: true,
    loginDisabled: false,
    phoneNum: '',
    // couponsUrl: '../Coupons/Coupons?userName'+phoneNum 
  },
  // 加载数据
  loadData(username) {
    var that = this;
    wx.request({
      url: app.baseNet + 'user/queryUserInfoByName?',
      data: {
        username: username
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        if (res.data.messcode == '001') {
          var noAuthLogin = wx.getStorageSync('noAuthLogin')
          var iconName = ''
          if (noAuthLogin == 'noAuthLogin') {
            // 存储用户头像和用户名
            wx.setStorageSync('latelyUserNickname', res.data.userInfoDto.nickName)
            wx.setStorageSync('userNickname', res.data.userInfoDto.nickName)
            if (res.data.userInfoDto.headPortrait) {
              iconName = res.data.userInfoDto.headPortrait
              wx.setStorageSync('latelyUserIcon', iconName)
              wx.setStorageSync('userIcon', iconName)
            } else {
              iconName = '/images/defaultIcon.png'
            }
            if (res.data.userInfoDto.couponToalPrice == null) {
              that.setData({
                couponToalPrice: '0',
              })
            } else {
              that.setData({
                couponToalPrice: res.data.userInfoDto.couponToalPrice,
              })
            }
            that.setData({
              loginTitle: res.data.userInfoDto.nickName,
              userIcon: iconName
            })
          } else {
            that.setData({
              couponToalPrice: res.data.userInfoDto.couponToalPrice
            })
          }
        } else if (res.data.messcode == '002') {
          
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var state = wx.getStorageSync('loginState')
    var loginTitle = wx.getStorageSync('userNickname')
    var userIcon = wx.getStorageSync('userIcon')
    var phoneNumber = wx.getStorageSync('phoneNumber')
    that.setData({
      phoneNum: phoneNumber,
    })
    if (state == 'auth') {
      that.setData({
        loginTitle: loginTitle,
        userIcon: userIcon,
        disabled: true,
        backHidden: false,
        loginDisabled: true
      })
    } else if (state == 'noAuth') {
      that.setData({
        disabled: true,
        backHidden: false,
        loginDisabled: true
      })
    }
    that.loadData(phoneNumber);
  },
  // 登录注册按钮点击事件
  loginBtnClick: function() {
    // 判断是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已授权
          var isFirstLogin = wx.getStorageSync('isNofirstLogin')
          if (isFirstLogin) {
            // 不是第一次登陆
            wx.navigateTo({
              url: '../fastLoginAndRegister/fastLoginAndRegister',
            })
          } else {
            // 是第一次登陆
            wx.navigateTo({
              url: '../loginAndRegister/loginAndRegister?jumpType=1',
            })
          }
        } else {
          // 未授权
          wx.navigateTo({
            url: '../loginAndRegister/loginAndRegister?jumpType=1',
          })
        }
      }
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // 
  phoneCall: function() {
    wx.makePhoneCall({
      phoneNumber: '400-150-5599',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 退出登录事件
  registerLogin: function() {
    var that = this
    wx.setStorageSync('userIcon', '')
    wx.setStorageSync('userNickname', '')
    wx.setStorageSync('phoneNumber', '')
    wx.setStorageSync('loginState', '')
    wx.setStorageSync('noAuthLogin', '')
    wx.setStorageSync('isNofirstLogin', true)
    that.loadData('');
    that.setData({
      disabled: false,
      backHidden: true,
      loginDisabled: false,
      couponToalPrice: 0,
      loginTitle: "点击登录/注册",
      userIcon: '/images/defaultIcon.png',
      phoneNum: '',
    })
  },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var phoneNumber = wx.getStorageSync('phoneNumber')
    if (firstLogin == false) {
      that.loadData(phoneNumber);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    firstLogin = false
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})